import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        //用滑动窗口的方法解决
        int left = 0;
        int right = 0;
        Map<Character, Integer> map = new HashMap<>();
        int len = 0;
        while(right < s.length()){
            //进窗口
            char key = s.charAt(right);
            int value = map.getOrDefault(key, 0);
            map.put(key, value + 1);
            //出窗口
            while(map.getOrDefault(key, 0) > 1){
                if(map.get(s.charAt(left)) > 1){
                    map.put(s.charAt(left), map.get(s.charAt(left)) - 1);
                }else{
                    map.remove(s.charAt(left));
                }
                left++;
            }
            //计算结果
            if(len < right - left + 1){
                len = right - left + 1;
            }
            right++;
        }
        return len;
    }
}